<!DOCTYPE html>
<html>

<head>
    <title>Online Store Seller Page</title>
    <link rel="stylesheet" type="text/css" href="css/style.css">
</head>

<body>
    <header>
        <h2>Online Store Seller page</h2>
    </header>
    <main>
        <div class="content">
            <h3>
                <?php
                session_start();
                $usr=$_SESSION["user"];
                echo "Your seller id is $usr";
                ?>
                <button type="button" class="styledBtn" onclick="window.location.href='LoginPage.html'">Logout</button>
            </h3>
        </div>
        <div class="content">
            <h2 id="top">Order management</h2>
            <hr>
            <div>
            <label style="font-weight: bold;color:#464646;">Search for order:</label>
                <br>
                <form action="sellerSearch.php" method="POST" class="searchForm">
                    <select name="mode">
                        <option value="oid">By Order id</option>
                        <option value="cid">By Customer id</option>
                    </select>
                    <input type="text" name="search" required>
                    <button type="submit">Search</button>
                </form>
                <a href="seller.php" style="float:right;">Display all</a>
                <br><br>
            </div>
            <!--table to display items-->
            <?php
            include "connectStoreDB.php";

            $search=$_POST["search"];
            $mode=$_POST["mode"];

            if($mode=="oid"){
                $sql = "SELECT * FROM orders WHERE order_id=$search;";
            }else{
                $sql = "SELECT * FROM orders WHERE customer_id=$search;";
            }

            $result = mysqli_query($conn, $sql);

            //check if result is empty
            if (mysqli_num_rows($result) > 0) {

                //print the heading of table
                echo "<table border='1'>";
                echo "<tr>";

                //print each column name
                while ($property = mysqli_fetch_field($result)) {
                    echo "<th>" . $property->name . "</th>";
                }
                echo "<th>Action</th></tr>";

                //get number of columns
                $fields = mysqli_num_fields($result);

                //Get each row data
                while ($row = mysqli_fetch_array($result)) {
                    echo "<tr>";
                    for ($f = 0; $f < $fields; $f++) {
                        echo "<td>" . $row[$f] . "</td>";
                    }
                    echo "<td><a href='order.php?oid=".$row[0]."'>Details</a></td>";
                }

                echo "</table>";
                echo mysqli_num_rows($result) . " results";
            } else {
                echo "0 results";
            }

            //close connection
            mysqli_close($conn);
            ?>
            <br>
            <hr>
        </div>
    </main>

</body>

</html>